<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/6/29 11:18
 * Modified Time: 2019/6/29 11:18
 * Version: 2.0
 */

namespace App\Modules\Activity\Repositories;


use App\Exceptions\AllValidatorException;
use App\Models\StoreActivity;
use App\Modules\Activity\Validators\ActivityValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;

class ActivityRepository extends BaseRepository
{
    public function model()
    {
        return StoreActivity::class;
    }

    public function validator()
    {
        return ActivityValidator::class;
    }

    /**
     * 新增公告
     * @param $store_id
     * @param $title
     * @param $playbill
     * @param $content
     * @param $expire
     * @return mixed
     */
    public function createActivity($store_id, $title, $playbill, $content, $expire)
    {
        $data = [
            'store_id' => $store_id,
            'title' => $title,
            'playbill' => $playbill,
            'content' => $content,
            'expire' => $expire,
        ];

        return $this->create($data);
    }

    /**
     * 编辑公告
     * @param $activity_id
     * @param $title
     * @param $playbill
     * @param $content
     * @return mixed
     */
    public function editActivity($activity_id, $title, $playbill, $content)
    {
        $this->checkExist($activity_id);

        $data = [];
        if ($title) {
            $data['title'] = $title;
        }
        if ($playbill) {
            $data['playbill'] = $playbill;
        }
        if ($content) {
            $data['content'] = $content;
        }
        return $this->update($data,$activity_id);
    }

    /**
     * 删除公告
     * @param $activity_id
     * @return int
     * @throws AllValidatorException
     */
    public function delActivity($activity_id)
    {
        $this->checkExist($activity_id);

        return $this->delete($activity_id);
    }

    /**
     * 验证公告是否存在
     * @param $activity_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function checkExist($activity_id)
    {
        $activity = $this->model->find($activity_id);

        if(!$activity){
            throw new AllValidatorException(['msg' => '公告不存在']);
        }

        return $activity;
    }
}